/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.uml21tospmif4;

import org.mitre.xmltransform.spmif.Device;
import org.mitre.xmltransform.spmif.SpmifOutputDocument;
import org.w3c.dom.Element;

public abstract class BaseNode {
	public BaseNode(String pUmlId, String pName, String pExecutesOn, String pInitiatedOn, NodeHierarchy pNodeHierarchy, Device pLocation) {
		mUmlId = pUmlId;
		mName = pName;
		mExecutesOn = pExecutesOn;
		mInitiatedOn = pInitiatedOn;
		mNodeHierarchy = pNodeHierarchy;
		if (pLocation == null) {
			mLocation = LOCATION_DEFAULT;
		} else {
			mLocation = pLocation;
		}
		mIdNumber = getNextId();
	}
		
	@Override
	public String toString() {
		return getClass().getSimpleName() + " (Name: " + getName() + "/ S-PMIF Node #: " + getIdNumber() + "/ Initiated on: " + initiatedOn() + "/ Executes on: " + executesOn() + ")";
	}

	public NodeHierarchy getNodeHierarchy() {
		return mNodeHierarchy;
	}
	public String getName() {
		return mName;
	}
	public String getUmlId() {
		return mUmlId;
	}
	public int getIdNumber() {
		return mIdNumber;
	}
	public String initiatedOn() {
		return mInitiatedOn;
	}
	public String executesOn() {
		return mExecutesOn;
	}
	public Device getLocation() {
		return mLocation;
	}

	public boolean isSameLocation(Device pLocation) {
		if (mLocation == null) { // not set yet
			mLocation = pLocation;
			return true;
		} else if (pLocation == null) {
			return LOCATION_NONE.equals(mLocation);
		} else {
			return mLocation.equals(pLocation);
		}
	}

	public boolean canHaveChildren() {
		return false;
	}
	public void addChild(BaseNode pChild) {
		throw new IllegalArgumentException("Node can not have children.");
	}

	public boolean isReflexive() {
		return mExecutesOn.equals(mInitiatedOn);
	}
	
	protected static int getNextId() {
		return cNextId++;
	}
	
	public String getOutermostSpmifId() {
		return getSpmifId();
	}
	public abstract String getSpmifId();
	public abstract void generateStep(Element pParentElement, SpmifOutputDocument pOutput);
	public abstract String generateArcs(Element pParentElement, String pFromNode, SpmifOutputDocument pOutput);
	
	private static final Device LOCATION_NONE = new Device("<<NONE>>", Device.TYPE_PROCESSING, null, 1);
	private static final Device LOCATION_DEFAULT = new Device("Default", Device.TYPE_PROCESSING, null, 1);

	private static int cNextId = 0;
	
	private NodeHierarchy mNodeHierarchy;
	private String mUmlId;
	private String mName;
	private String mExecutesOn;
	private String mInitiatedOn;
	private Device mLocation;
	private int mIdNumber;
}
